from random import randrange

from django.core.mail import send_mail

from ttsx.settings import EMAIL_FROM
from user.models import EmailVerifyCode


def get_random_code(code_length):
    code_source = '1234567890qwertyuiopasdfghjklzxcvbnm'
    code = ''

    for i in range(code_length):
        str = code_source[randrange(0, len(code_source))]
        code += str

    return code


def send_email_code(email, send_type):
    code = get_random_code(6)
    a = EmailVerifyCode()
    a.email = email
    a.send_type = send_type
    a.code = code
    a.save()

    send_title = ''
    send_body = ''
    if send_type == 1:
        send_title = '欢迎注册天天生鲜（赵向阳版本）：'
        send_body = '请点击以下链接进行激活您的账号：\n http://127.0.0.1:8000/user_activate/' + code
        send_mail(send_title, send_body, EMAIL_FROM, [email])
